package com.taotao.content.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.JsonUtils;
import com.taotao.content.redis.JedisClient;
import com.taotao.content.service.ContentService;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.pojo.TbContentExample.Criteria;
@Service
public class ContentServiceImpl implements ContentService{
	@Autowired
	private TbContentMapper  tbContentMapper;
	@Autowired
	private JedisClient jedisClient;
	@Value("${CONTENT_KEY}")
	private String CONTENT_KEY;
	//显示商品列表
	@Override
	public EasyUIDataGridResult getContentList(Long categoryId, Integer page, Integer rows) {
		//设置分页信息
		PageHelper.startPage(page, rows);
		//设置查询条件
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(categoryId);
		List<TbContent> contentList = tbContentMapper.selectByExample(example);
		EasyUIDataGridResult dataGridResult = new EasyUIDataGridResult();
		//获取分页信息并封装到结果数据中
		PageInfo<TbContent> pageInfo = new PageInfo<>(contentList);
		dataGridResult.setTotal(pageInfo.getTotal());
		dataGridResult.setRows(contentList);
		return dataGridResult;
	}
	//内容管理添加
	@Override
	public TaotaoResult insertContent(TbContent tbContent) {
		tbContent.setCreated(new Date());
		tbContent.setUpdated(new Date());
		//插入数据
		tbContentMapper.insert(tbContent);
		//同步redis的数据
		jedisClient.hdel(CONTENT_KEY,tbContent.getCategoryId().toString());
		return TaotaoResult.ok();
	}
	//首页轮播图显示
	@Override
	public List<TbContent> getContentList(Long id) {
		try {
			String json = jedisClient.hget(CONTENT_KEY, id+"");
			if(StringUtils.isNotBlank(json)){
				return JsonUtils.jsonToList(json, TbContent.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(id);
		List<TbContent> contentList = tbContentMapper.selectByExample(example);
		try {
			jedisClient.hset(CONTENT_KEY, id+"", JsonUtils.objectToJson(contentList));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return contentList;
	}
	//删除网站组中内容
	@Override
	public TaotaoResult delContentList(String ids) {
		String[] idStr = ids.split(",");
		for (String s : idStr) {
			Long id = Long.parseLong(s);
			tbContentMapper.deleteByPrimaryKey(id);
		}
		return TaotaoResult.ok();
	}
	//更新内容
	@Override
	public TaotaoResult updateContent(TbContent tbContent) {
		TbContent t_tbContent = tbContentMapper.selectByPrimaryKey(tbContent.getId());
		tbContent.setUpdated(new Date());
		tbContent.setCreated(t_tbContent.getCreated());
		tbContentMapper.updateByPrimaryKeyWithBLOBs(tbContent);
		return TaotaoResult.ok();
	}
	//获取商品描述
	@Override
	public TaotaoResult getContentContent(Long id) {
		TbContent tbContent = tbContentMapper.selectByPrimaryKey(id);
		return TaotaoResult.ok(tbContent);
	}
}
